package com.thread.demo.controller;

import com.thread.demo.config.ThreadLocalConfig;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author hh
 * @since 2023/3/16
 */
@RestController
@RequestMapping("threadLocal")
public class ThreadLocalController {

    @GetMapping("test")
    public String test(String name) {
        ThreadLocalConfig.threadLocal.set(name);
        return getMethodValue();
    }

    private String getMethodValue() {
        try {
            return ThreadLocalConfig.threadLocal.get();
        } finally {
            ThreadLocalConfig.threadLocal.remove();
        }
    }

}
